package com.wu.flandre.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.flandre.dao.ApiCallCensusDaoMapper;
import com.wu.flandre.entity.common.ApiCallMsgRecordEntity;
import com.wu.flandre.entity.table.ApiCallCensus;
import com.wu.flandre.service.ApiCallCensusService;
import com.wu.flandre.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wu
 * @since 2019-12-14
 */
@Service
@Slf4j
public class ApiCallCensusServiceImpl extends ServiceImpl<ApiCallCensusDaoMapper, ApiCallCensus> implements ApiCallCensusService {

    @Autowired
    private ApiCallCensusDaoMapper apiCallCensusDaoMapper;
    @Autowired
    private CommonUtil commonUtil;

    @Override
    public void apiCallInfoRecordDataBase(ApiCallMsgRecordEntity recordEntity) {
        LocalDateTime startTime = LocalDateTime.ofEpochSecond(recordEntity.getStartTime() / 1000, 0, ZoneOffset.ofHours(8));
        LocalDateTime endTime = LocalDateTime.ofEpochSecond(recordEntity.getEndTime() / 1000, 0, ZoneOffset.ofHours(8));

        ApiCallCensus apiCallCensus = new ApiCallCensus();
        apiCallCensus.setApiCallId(commonUtil.getUUID());
        apiCallCensus.setApiMethod(recordEntity.getApiMethod());
        apiCallCensus.setApiName(recordEntity.getApiName());
        apiCallCensus.setApiPath(recordEntity.getApiPath());
        apiCallCensus.setApiCallStart(startTime);
        apiCallCensus.setApiCallEnd(endTime);
        apiCallCensus.setApiUseTime((int) (recordEntity.getEndTime() - recordEntity.getStartTime()));

        int state = apiCallCensusDaoMapper.insert(apiCallCensus);
        if (state != 1) {
            log.warn("接口调用信息插入数据库失败");
        }
    }
}
